﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Library;
using Lamex.Interface;

namespace Lamex.mNC.UI
{
    public partial class frmNC_AL_Scan_Report : frmBaseChild
    {
        string strSql;
        DataTable dt;

        public frmNC_AL_Scan_Report()
        {
            InitializeComponent();
        }

        private void frmNC_AL_Scan_Report_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = this.Width * 9 / 20;
            splitContainerControl2.SplitterPosition = this.Width * 4 / 20;
        }

        private void frmNC_AL_Scan_Report_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
            refreshData();
        }

        //GRIDVIEW页脚汇总设置
        private void TotalSum()
        {
            gridView1.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            gridView1.Columns[0].SummaryItem.DisplayFormat = "合计：";

            gridView1.Columns[1].DisplayFormat.FormatString = "{0:#%}";
            //"{0:#%}"
            //gridView1.Columns[3].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Average;
            //gridView1.Columns[3].SummaryItem.DisplayFormat = "{0:N}";
            // this.gridView1.Columns[1].Width = 80;

            //if (gridView1.Columns.Contains(gridView1.Columns[2]))
            //{
            //    gridView1.Columns[2].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
            //    this.gridView1.Columns[2].SummaryItem.DisplayFormat = "{0:N}";
            //    this.gridView1.Columns[2].Width = 80;
            //}
        }

        private void refreshData()
        {
            try
            {
                gridView1.OptionsBehavior.Editable = false;
                gridView2.OptionsBehavior.Editable = false;
                gridView3.OptionsBehavior.Editable = false;

                DateTime now = DateTime.Now;

                String strDT1, strDT2;

                //取当前周及后推四周
                strDT1 = now.AddDays(-(int)now.DayOfWeek + 1 - 28).ToString("yyyy/MM/dd");
                strDT2 = now.AddDays(7 - (int)now.DayOfWeek + 0).ToString("yyyy/MM/dd");

                //            strSql = @"SELECT SCAN_DATE AS 入仓扫描日期,
                //FOLLOW_AMOUNT AS [当日入仓过CNC加工数(跟进表+部件号)],
                //CONVERTED_NOS AS 可上CNC用NC代码加工数,
                //case FOLLOW_AMOUNT when 0 then '0%' else cast(convert(decimal(18,1),(convert(decimal(18,1),CONVERTED_NOS)/FOLLOW_AMOUNT*100)) as varchar) + '%' end as NC转码率,
                //PROCESSED_NOS AS 生产实际扫码加工数,
                //case FOLLOW_AMOUNT when 0 then '0%' else cast(convert(decimal(18,1),(convert(decimal(18,1),PROCESSED_NOS)/CONVERTED_NOS*100)) as varchar) + '%' end as 生产扫码加工率
                //FROM NC_PN_AL_Manufacture_Process_Ratio
                //WHERE SCAN_DATE>='" + strDT1 + @"'
                //AND SCAN_DATE<='" + strDT2 + @"'
                //ORDER BY SCAN_DATE DESC";

                strSql = @"SELECT 1 AS SN_NO,CONVERT(varchar(100),SCAN_DATE,23) AS SCAN_DATE,
FOLLOW_AMOUNT,
CONVERTED_NOS,
case FOLLOW_AMOUNT when 0 then 0 else convert(decimal(18,3),convert(float,CONVERTED_NOS)/FOLLOW_AMOUNT) end as CONVERTED_RATIO,
PROCESSED_NOS AS 生产实际扫码加工数,
case FOLLOW_AMOUNT when 0 then 0 else convert(decimal(18,3),convert(float,PROCESSED_NOS)/CONVERTED_NOS) end as PROCESSED_RATIO
FROM NC_PN_AL_Manufacture_Process_Ratio
WHERE SCAN_DATE>='" + strDT1 + @"'
AND SCAN_DATE<='" + strDT2 + @"'
UNION
SELECT  2,'小计:',
SUM(FOLLOW_AMOUNT),
SUM(CONVERTED_NOS),
convert(decimal(18,3),convert(float,SUM(CONVERTED_NOS))/SUM(FOLLOW_AMOUNT)),
SUM(PROCESSED_NOS),
convert(decimal(18,3),convert(float,SUM(PROCESSED_NOS))/SUM(CONVERTED_NOS))
FROM NC_PN_AL_Manufacture_Process_Ratio
WHERE SCAN_DATE>='" + strDT1 + @"'
AND SCAN_DATE<='" + strDT2 + @"'
ORDER BY SN_NO,SCAN_DATE DESC";

                dt = BaseBuildSql.GetDataTableBySql(strSql);
                gridControl1.DataSource = dt;

                #region GRIDVIEW标准化列格式设置

                /*因通过页脚求转码率的平均值，与通过数量汇总，再求转码率，有差异
                    * 所以改为直接在SQL查询中拼接“求和或平均”
                    * 
                //gridView1.OptionsView.ShowFooter = true;
                gridView1.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom;
                gridView1.Columns[0].SummaryItem.DisplayFormat = "求和或平均：";//固定显示
                //gridView1.Columns[0].SummaryItem.FieldName = "";
                //gridView1.Columns[0].SummaryItem.DisplayFormat = "合计：";//固定显示

                gridView1.Columns[1].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                gridView1.Columns[1].SummaryItem.DisplayFormat = "SUM:{0:0}";//整数
                gridView1.Columns[2].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                gridView1.Columns[2].SummaryItem.DisplayFormat = "SUM:{0:0}";//整数

                //2位小数
                //gridView1.Columns[3].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                //gridView1.Columns[3].DisplayFormat.FormatString = "N2";
                //百分比
                gridView1.Columns[3].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                gridView1.Columns[3].DisplayFormat.FormatString = "{0:0.#%}";
                gridView1.Columns[3].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Average;
                gridView1.Columns[3].SummaryItem.DisplayFormat = "AVG:{0:0.#%}";

                gridView1.Columns[4].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                gridView1.Columns[4].SummaryItem.DisplayFormat = "SUM:{0:0}";//整数

                gridView1.Columns[5].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                gridView1.Columns[5].DisplayFormat.FormatString = "{0:0.#%}";
                gridView1.Columns[5].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Average;
                gridView1.Columns[5].SummaryItem.DisplayFormat = "AVG:{0:0.#%}";

                */

                #endregion

                strSql = @"SELECT SCAN_TIME AS 加工扫描时间,SUM(PROCESS_QTY) AS 加工数量
FROM
(SELECT FORMAT(SCAN_TIME,'yyyy-MM-dd') AS SCAN_TIME,PROCESS_QTY
FROM NC_PN_AL_Manufacture_Process_Record
WHERE SCAN_TIME>='" + strDT1 + @"'
AND SCAN_TIME<='" + strDT2 + @"') AS a
GROUP BY SCAN_TIME
ORDER BY 加工扫描时间 DESC";

                dt = BaseBuildSql.GetDataTableBySql(strSql);
                gridControl2.DataSource = dt;

                gridView2.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom;
                gridView2.Columns[0].SummaryItem.DisplayFormat = "合计：";//固定显示
                ////因字段本身为int，可以不用指定
                //gridView2.Columns[1].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                //gridView2.Columns[1].DisplayFormat.FormatString = "N0";//整数，如果是"N"，则默认2位小数，效果同"N2"
                gridView2.Columns[1].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                gridView2.Columns[1].SummaryItem.DisplayFormat = "{0:0}";


                strSql = @"SELECT FORMAT(SCAN_TIME,'yyyy-MM-dd') AS 扫描加工时间,BARCODE_NO AS 条码,
MACHINE_NO as 机台号,PROCESS_QTY AS 加工数量
FROM NC_PN_AL_Manufacture_Process_Record
WHERE SCAN_TIME>='" + strDT1 + @"'
AND SCAN_TIME<='" + strDT2 + @"'
ORDER BY SCAN_TIME DESC";

                dt = BaseBuildSql.GetDataTableBySql(strSql);
                gridControl3.DataSource = dt;

                gridView3.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom;
                gridView3.Columns[0].SummaryItem.DisplayFormat = "合计：";//固定显示
                ////因字段本身为int，可以不用指定
                //gridView3.Columns[1].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                //gridView3.Columns[1].DisplayFormat.FormatString = "N0";
                gridView3.Columns[3].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                gridView3.Columns[3].SummaryItem.DisplayFormat = "{0:0}";

                //TotalSum();
            }
            catch (Exception e)
            {
                MessageBox.Show("最近无扫描记录！" + "\r\n" + e.ToString());
            }
        }

        public override void DoView(IButtonInfo sender)
        {
            refreshData();
            base.DoView(sender);
        }
    }
}